/**
 * 
 */
package org.vsg.common.pagination.wrapper;

import java.util.Hashtable;
import java.util.Iterator;

import org.vsg.common.pagination.Pagination;

/**
 * @author bill vison
 *
 */
public class GenericWebPaginationWrapper extends AbstractPaginationWrapper implements WebPaginationWrapper{
	
	protected String url;
	
	protected Hashtable urlParams = new Hashtable();
	
	protected String goToPageParamName;
	
	/**
	 * append the url for the parameter
	 * @param key
	 * @param value
	 */
	public void addParamToUrl(String key , String[] values) {
		urlParams.put(key,values);
	}
	

	public void removeParamFromUrl(String key) {
		urlParams.remove(key);
	}
	
	/**
	 * @return Returns the pagination.
	 */
	public Pagination getPagination() {
		return super.getPagination();
	}

	/**
	 * @return Returns the url.
	 */
	public String getUrl() {
		return url;
	}
	/**
	 * @param url The url to set.
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return Returns the goToPageParamName.
	 */
	public String getGoToPageParamName() {
		return goToPageParamName;
	}
	
	/**
	 * @param goToPageParamName The goToPageParamName to set.
	 */
	public void setGoToPageParamName(String goToPageParamName) {
		this.goToPageParamName = goToPageParamName;
	}

	// ------------------------------------------------- the following method is set for page
	public String getFirstPageUrl() {
		int firstPageIndex = 0;
		Pagination pagination = super.getPagination();
		
		if (pagination.getTotalRecord() > 0) {
			if (pagination.getPageIndex() == 0) {
				firstPageIndex = 1;
			} else {
				firstPageIndex = pagination.getFirstPageIndex() + 1;
			}
		}
		
		
		// if the url string is end with character "?" ,dont add the character "?" in the end
		String url = this.url.endsWith("?") ? this.url:(this.url+"?");
		
		StringBuffer paramsUrlStr = new StringBuffer(url );
		paramsUrlStr.append("&").append(goToPageParamName).append("=").append(firstPageIndex);
		// add another parameter
		paramsUrlStr.append(getAdditionalParams());

		return paramsUrlStr.toString();
	}

	public String getLastPageUrl() {
		int lastPageIndex = 0;
		Pagination pagination = super.getPagination();		
		if (pagination.getTotalRecord() > 0) {
			lastPageIndex = pagination.getLastPageIndex() + 1;
		}
		
		// if the url string is end with character "?" ,dont add the character "?" in the end
		String url = this.url.endsWith("?") ? this.url:(this.url+"?");
		
		StringBuffer paramsUrlStr = new StringBuffer(url);
		paramsUrlStr.append("&").append(goToPageParamName).append("=").append(lastPageIndex);


		// add another parameter
		paramsUrlStr.append(getAdditionalParams());		
		return paramsUrlStr.toString();
	}
	
	public String getNextPageUrl() {
		int nextPageUrl = 0;
		Pagination pagination = super.getPagination();		
		if (pagination.getTotalRecord() > 0) {
			if (pagination.getPageIndex() == 0) {
				
			}
			nextPageUrl = (pagination.getPageIndex() + 1) + 1;
			if (pagination.getTotalPages() <=  1) {
				nextPageUrl = 1;
			} else {
				if (nextPageUrl > pagination.getLastPageIndex()) {
					// add 1 is fix for the page viewer
					nextPageUrl = pagination.getLastPageIndex() + 1;
				}
			}
		}
		

		
		// if the url string is end with character "?" ,dont add the character "?" in the end
		String url = this.url.endsWith("?") ? this.url:(this.url+"?");
		
		StringBuffer paramsUrlStr = new StringBuffer(url);
		paramsUrlStr.append("&").append(goToPageParamName).append("=").append(nextPageUrl);
		// add another parameter
		paramsUrlStr.append(getAdditionalParams());
		return paramsUrlStr.toString();		
	}
	
	public String getPrevPageUrl() {
		int prevPageUrl = 0;
		Pagination pagination = super.getPagination();		
		if (pagination.getTotalRecord() > 0) {
			prevPageUrl = (pagination.getPageIndex() - 1) + 1;
			if (pagination.getTotalPages() <= 1) {
				prevPageUrl = 1;
			} else {
				if (prevPageUrl < 1) {
					prevPageUrl = 1;
				}
			}
		}
		
		// if the url string is end with character "?" ,dont add the character "?" in the end
		String url = this.url.endsWith("?") ? this.url:(this.url+"?");		
		StringBuffer paramsUrlStr = new StringBuffer(url);
		paramsUrlStr.append("&").append(goToPageParamName).append("=").append(prevPageUrl);
		// add another parameter
		paramsUrlStr.append(getAdditionalParams());
		return paramsUrlStr.toString();				
	}
	
	public String getOtherParamsUrl() {
		StringBuffer paramsUrlStr = new StringBuffer();		
		// add another parameter
		paramsUrlStr.append(getAdditionalParams());
		return paramsUrlStr.toString();		
	}

	/**
	 * show the number page url of page num
	 * @param pageNum
	 * @return
	 */
	public String getNumPageUrl(int pageIndex) {
		int pageNum = 0;
		Pagination pagination = super.getPagination();		
		if (pagination.getTotalRecord() > 0) {
			if (pagination.getTotalPages() > 0) {
				pageNum = pageIndex + 1;
			} else {
				pageNum = 1;
			}
		}
		
		// if the url string is end with character "?" ,dont add the character "?" in the end
		String url = this.url.endsWith("?") ? this.url:(this.url+"?");
		
		StringBuffer paramsUrlStr = new StringBuffer(url);
		paramsUrlStr.append("&").append(goToPageParamName).append("=").append(pageNum);
		// add another parameter
		paramsUrlStr.append(getAdditionalParams());
		return paramsUrlStr.toString();		
	}
	
	public int getNumPage(int pageIndex) {
		int pageNum = 0;
		Pagination pagination = super.getPagination();		
		if (pagination.getTotalRecord() > 0) {
			if (pagination.getTotalPages() > 0) {
				pageNum = pageIndex + 1;
			} else {
				pageNum = 1;
			}
		}
		return pageNum;
	}
	
	// ------------------------------------------------- the following method is set for private 
	protected String getAdditionalParams() {
		StringBuffer paramsUrlStr = new StringBuffer();
		Iterator iter = urlParams.keySet().iterator();
		String paramName = null;
		String[] parameterValues = null;
		String value = null;
		while (iter.hasNext()) {
			paramName = (String)iter.next();
			parameterValues = (String[])urlParams.get(paramName);
			for (int i = 0 ; i < parameterValues.length ; i++) {
				value = parameterValues[i];
				value = (value == null) ? "" : value;
				paramsUrlStr.append("&").append(paramName).append("=").append(value);	
			}
		}		
		return paramsUrlStr.toString();
	}
}
